const Controller = require('egg').Controller;

const fs = require('fs')

const MockData = require('../../mock/index.js')

const JsonData = require('./data.json')

class HomeController extends Controller {
  //往数据库添加数据
  async mock() {
    const { ctx, app } = this;

    // fs.writeFileSync(`${__dirname}/data.json`, JSON.stringify(MockData), () => { })

    const data = await app.mysql.insert('city', (JsonData))

    ctx.body = {
      code: 0,
      msg: '请求成功',
      data
    }
  }

  //获取楼市资讯数据
  async getRealList() {
    const { ctx, app } = this;

    const data = await app.mysql.query('select * from 楼市资讯')

    ctx.body = {
      code: 0,
      msg: '请求成功',
      data
    }
  }

  //获取热门楼盘数据
  async getHotList() {
    const { ctx, app } = this;

    const data = await app.mysql.query('select * from 热门楼盘')

    ctx.body = {
      code: 0,
      msg: '请求成功',
      data
    }
  }

  //获取经纪人数据
  async getBroker() {
    const { ctx, app } = this;

    const data = await app.mysql.query('select * from 经纪人')

    ctx.body = {
      code: 0,
      msg: '请求成功',
      data
    }
  }

  //获取二手房数据
  async getResoldHome() {
    const { ctx, app } = this;

    const { value } = ctx.request.body

    try {
      // const data = await app.mysql.query(`select * from list where name like '%${value}%'`)
      const data = await app.mysql.query(`select * from 二手房 where title like '%${value}%'`)

      ctx.body = {
        code: 0,
        msg: '请求成功',
        data
      }
    } catch (error) {
      ctx.body = {
        code: error
      }
    }

  }
}

module.exports = HomeController;
